package com.health.activity.tijian;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.health.FWMYDPJActivity;
import com.health.NovaActivity;
import com.health.NovaApplication;
import com.health.R;
import com.health.db.FDbManager;
import com.health.tablebean.OLDER_SELFCAREABILITY;
import com.health.tools.GlobalConts;
import com.health.view.spinner.NiceSpinner;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * 老年人自理评估问卷
 * 
 * @author 田相伟
 *
 */
public class YBQK_ZiLiWenJuanActivity extends NovaActivity {
	private static final String TAG = "YBQK_ZiLiWenJuanActivity";
	public static YBQK_ZiLiWenJuanActivity mYBQK_ZiLiWenJuanActivity;
	public NiceSpinner jinCan;
	public NiceSpinner suXi;
	public NiceSpinner chuanYi;
	public NiceSpinner ruCe;
	public NiceSpinner huoDong;
	public NiceSpinner yisheng;
	public TextView score;
	public Button save;
	public Button cancle;
	private FDbManager fdbmanger = null;
	int jinCanIndex;
	int suXiIndex;
	int chuanYiIndex;
	int ruCeIndex;
	int huoDongIndex;
	int jinCanScore = 0;
	int shuXiScore = 0;
	int chuanyiScore = 0;
	int ruceScore = 0;
	int huodongScore = 0;
	int scoreString;
	OLDER_SELFCAREABILITY older_SELFCAREABILITY;
	String IDCARD;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ybqk_zili_wenjuan);
		IDCARD = NovaApplication.getUserInfo().getIDCARD();
		fdbmanger = NovaApplication.fdbmanger;
		findViews();
		setListeners();
		try {
			setData();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	@Override
	public void findViews() {
		older_SELFCAREABILITY = new OLDER_SELFCAREABILITY();
		older_SELFCAREABILITY.setIDCARD(IDCARD);
		jinCan = (NiceSpinner) findViewById(R.id.jincan_spinner);
		suXi = (NiceSpinner) findViewById(R.id.shuxi_spinner);
		chuanYi = (NiceSpinner) findViewById(R.id.chuanyi_spinner);
		ruCe = (NiceSpinner) findViewById(R.id.ruce_spinner);
		huoDong = (NiceSpinner) findViewById(R.id.huodong_spinner);
		yisheng = (NiceSpinner) findViewById(R.id.yisheng_edit);
		score = (TextView) findViewById(R.id.score_edit);
		save = (Button) findViewById(R.id.save);
		cancle = (Button) findViewById(R.id.cancle);
		setNiceSpinner();
		mYBQK_ZiLiWenJuanActivity = this;
	}

	public void setNiceSpinner() {
		// List<BD_DD_INFO> myList =
		// FDbManager.getInstance(this).findAllByWhereInInfoDB(BD_DD_INFO.class,
		// "PARENTID = 'DM100-03' order by cast(SORT as int) asc");
		List<String> myList2 = new ArrayList<String>();
		myList2.add("可自理");
		myList2.add("轻度依赖");
		myList2.add("中度依赖");
		myList2.add("不可自理");
		// for (int i = 0; i < 4; i++) {
		// myList2.add(myList.get(i).getDATAINFO());
		// }
		jinCan.attachDataSource(myList2);
		jinCan.setText(myList2.get(0));
		suXi.attachDataSource(myList2);
		suXi.setText(myList2.get(0));
		chuanYi.attachDataSource(myList2);
		chuanYi.setText(myList2.get(0));
		ruCe.attachDataSource(myList2);
		ruCe.setText(myList2.get(0));
		huoDong.attachDataSource(myList2);
		huoDong.setText(myList2.get(0));
		// 医生列表
		GlobalConts.doctorList(yisheng);
	}

	@Override
	public void setListeners() {
		cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
		jinCan.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if (position == 0) {
					jinCanScore = 0;
				} else if (position == 1) {
					jinCanScore = 0;
				} else if (position == 2) {
					jinCanScore = 3;
				} else if (position == 3) {
					jinCanScore = 5;
				}
				Log.e(TAG, "" + jinCanScore);
				setScore();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
		suXi.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if (position == 0) {
					shuXiScore = 0;
				} else if (position == 1) {
					shuXiScore = 1;
				} else if (position == 2) {
					shuXiScore = 3;
				} else if (position == 3) {
					shuXiScore = 7;
				}
				setScore();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
		chuanYi.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if (position == 0) {
					chuanyiScore = 0;
				} else if (position == 1) {
					chuanyiScore = 0;
				} else if (position == 2) {
					chuanyiScore = 3;
				} else if (position == 3) {
					chuanyiScore = 5;
				}
				setScore();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
		ruCe.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if (position == 0) {
					ruceScore = 0;
				} else if (position == 1) {
					ruceScore = 1;
				} else if (position == 2) {
					ruceScore = 5;
				} else if (position == 3) {
					ruceScore = 10;

				}
				setScore();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
		huoDong.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				if (position == 0) {
					huodongScore = 0;
				} else if (position == 1) {
					huodongScore = 1;
				} else if (position == 2) {
					huodongScore = 5;
				} else if (position == 3) {
					huodongScore = 10;
				}
				setScore();

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});
		save.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				jinCanIndex = jinCan.getSelectedIndex();
				suXiIndex = suXi.getSelectedIndex();
				chuanYiIndex = chuanYi.getSelectedIndex();
				ruCeIndex = ruCe.getSelectedIndex();
				huoDongIndex = huoDong.getSelectedIndex();
				older_SELFCAREABILITY.setDINE(jinCanIndex + "");
				older_SELFCAREABILITY.setGROMING(suXiIndex + "");
				older_SELFCAREABILITY.setDRESSING(chuanYiIndex + "");
				older_SELFCAREABILITY.setTOLET(ruCeIndex + "");
				older_SELFCAREABILITY.setACTIVITY(huoDongIndex + "");
				older_SELFCAREABILITY.setTOTALSCORE(scoreString + "");
				older_SELFCAREABILITY
						.setFOLLOWUPDOCTOR(NovaApplication.getDoctorList().get(yisheng.getSelectedIndex()).getUSER_ID());
				older_SELFCAREABILITY
						.setLAST_UPDATE_BY(NovaApplication.getDoctorList().get(yisheng.getSelectedIndex()).getUSER_ID());
				older_SELFCAREABILITY.setLAST_UPDATE_DATE(new Date());
				older_SELFCAREABILITY.setIS_UPDATE("Y");
				if (fdbmanger.findAllByWhere(OLDER_SELFCAREABILITY.class, "IDCARD='" + IDCARD + "'").size() > 0) {
					older_SELFCAREABILITY
							.setMODIFIED(NovaApplication.getDoctorList().get(yisheng.getSelectedIndex()).getUSER_ID());
					older_SELFCAREABILITY.setUPDATEORG(NovaApplication.getUserInfo().getCREATEORG());
					fdbmanger.updata(older_SELFCAREABILITY, "IDCARD='" + IDCARD + "'");
				} else {
					older_SELFCAREABILITY.setCREATED_BY(NovaApplication.getUserInfo().getCREATED_BY());
					older_SELFCAREABILITY.setCREATED_DATE(new Date());
					older_SELFCAREABILITY.setCREATEORG(NovaApplication.getUserInfo().getCREATEORG());
					older_SELFCAREABILITY.setCREATER(NovaApplication.getUserInfo().getCREATER());
					fdbmanger.save(older_SELFCAREABILITY);
				}
				// showTextDialog("保存成功");
				if (GlobalConts.TYPE_JTYS.equals(NovaApplication.getType())) {
					new AlertDialog.Builder(YBQK_ZiLiWenJuanActivity.this).setTitle("保存成功").setMessage("进行满意度评价")
							.setPositiveButton("确定", new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							Intent intent = new Intent(YBQK_ZiLiWenJuanActivity.this, FWMYDPJActivity.class);
							intent.putExtra("type", "05");
							intent.putExtra("szfs", "");
							startActivity(intent);
						}
					}).show();
				} else if (NovaApplication.getType().equals("TYPE_GJJBGGWS")) {
					showTextDialog("保存成功");
				}
				// finish();
			}
		});

	}

	// 设置总分数
	public void setScore() {
		scoreString = jinCanScore + shuXiScore + chuanyiScore + ruceScore + huodongScore;
		Log.e(TAG, "" + jinCanScore + "\n" + shuXiScore + "\n" + chuanyiScore + "\n" + ruceScore + "\n" + huodongScore);
		score.setText(scoreString + "");
	}

	/**
	 * 清空数据
	 * 
	 * @author 王婷婷
	 * @date 2017年6月24日 上午11:33:23
	 */
	private void cleanData() {
		jinCanScore = 0;
		shuXiScore = 0;
		chuanyiScore = 0;
		ruceScore = 0;
		huodongScore = 0;
	}

	private void setData() {
		List<OLDER_SELFCAREABILITY> list = fdbmanger.findAllByWhere(OLDER_SELFCAREABILITY.class,
				"IDCARD='" + IDCARD + "'");
		if (list.size() > 0) {
			OLDER_SELFCAREABILITY older_SELFCAREABILITY1 = list.get(0);
			jinCan.setSelectedIndex(Integer.parseInt(older_SELFCAREABILITY1.getDINE()));
			int position1 = Integer.parseInt(older_SELFCAREABILITY1.getDINE());
			if (position1 == 0) {
				jinCanScore = 0;
			} else if (position1 == 1) {
				jinCanScore = 0;
			} else if (position1 == 2) {
				jinCanScore = 3;
			} else if (position1 == 3) {
				jinCanScore = 5;
			}
			suXi.setSelectedIndex(Integer.parseInt(older_SELFCAREABILITY1.getGROMING()));
			int position2 = Integer.parseInt(older_SELFCAREABILITY1.getGROMING());
			if (position2 == 0) {
				shuXiScore = 0;
			} else if (position2 == 1) {
				shuXiScore = 1;
			} else if (position2 == 2) {
				shuXiScore = 3;
			} else if (position2 == 3) {
				shuXiScore = 7;
			}
			chuanYi.setSelectedIndex(Integer.parseInt(older_SELFCAREABILITY1.getDRESSING()));
			int position3 = Integer.parseInt(older_SELFCAREABILITY1.getDRESSING());
			if (position3 == 0) {
				chuanyiScore = 0;
			} else if (position3 == 1) {
				chuanyiScore = 0;
			} else if (position3 == 2) {
				chuanyiScore = 3;
			} else if (position3 == 3) {
				chuanyiScore = 5;
			}
			ruCe.setSelectedIndex(Integer.parseInt(older_SELFCAREABILITY1.getTOLET()));
			int position4 = Integer.parseInt(older_SELFCAREABILITY1.getTOLET());
			if (position4 == 0) {
				ruceScore = 0;
			} else if (position4 == 1) {
				ruceScore = 1;
			} else if (position4 == 2) {
				ruceScore = 5;
			} else if (position4 == 3) {
				ruceScore = 10;

			}
			huoDong.setSelectedIndex(Integer.parseInt(older_SELFCAREABILITY1.getACTIVITY()));
			int position5 = Integer.parseInt(older_SELFCAREABILITY1.getACTIVITY());
			if (position5 == 0) {
				huodongScore = 0;
			} else if (position5 == 1) {
				huodongScore = 1;
			} else if (position5 == 2) {
				huodongScore = 5;
			} else if (position5 == 3) {
				huodongScore = 10;
			}
			for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
				if (older_SELFCAREABILITY1.getFOLLOWUPDOCTOR().equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
					yisheng.setSelectedIndex(i);
				}

			}
			score.setText(older_SELFCAREABILITY1.getTOTALSCORE() + "");
		} else {
			for (int i = 0; i < NovaApplication.getDoctorList().size(); i++) {
				if (NovaApplication.getUserInfo().getCREATED_BY()
						.equals(NovaApplication.getDoctorList().get(i).getUSER_ID())) {
					yisheng.setSelectedIndex(i);
				}

			}
		}

	}
}
